Option Strict Off
Option Explicit On
Friend Class frmImport
	Inherits System.Windows.Forms.Form
	
	Dim mobjPreview As GenObjArr
	Dim mlngFileType As Integer
	Dim mlngColumnCount As Integer
	Const TabDelimited As Integer = 1
	Const CommaDelimited As Integer = 2
	Const NotDelimited As Integer = 3
	
	
	'UPGRADE_WARNING: Event chkHeaderRow.CheckStateChanged may fire when form is initialized. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="88B12AE1-6DE0-48A0-86F1-60C0686C026A"'
	Private Sub chkHeaderRow_CheckStateChanged(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles chkHeaderRow.CheckStateChanged
		UpdateColumnNames()
		DisplayPreviewText()
		SetGUIButtons()
	End Sub
	
	'UPGRADE_WARNING: Event chkStripQuotes.CheckStateChanged may fire when form is initialized. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="88B12AE1-6DE0-48A0-86F1-60C0686C026A"'
	Private Sub chkStripQuotes_CheckStateChanged(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles chkStripQuotes.CheckStateChanged
		DisplayPreviewText()
		SetGUIButtons()
	End Sub
	
	Private Sub cmdCancel_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdCancel.Click
		Me.Close()
	End Sub
	
	Private Sub cmdImport_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdImport.Click
		
		Dim objFSO As Scripting.FileSystemObject
		objFSO = New Scripting.FileSystemObject
		
		If objFSO.FileExists(txtFileToImport.Text) = False Then
			MsgBox("Can not access """ & txtFileToImport.Text & """.", MsgBoxStyle.Information + MsgBoxStyle.OKOnly, "Error")
			Exit Sub
		End If
		
		Dim objInputFile As Scripting.TextStream
		objInputFile = objFSO.OpenTextFile(txtFileToImport.Text, Scripting.IOMode.ForReading, False, Scripting.Tristate.TristateUseDefault)
		
		Dim strBuffer As String
		strBuffer = objInputFile.ReadAll
		
		Dim aryBuffer() As String
		
		If InStr(1, strBuffer, Chr(13) & Chr(10), CompareMethod.Binary) > 0 Then
			aryBuffer = Split(strBuffer, Chr(13) & Chr(10),  , CompareMethod.Binary)
		Else
			aryBuffer = Split(strBuffer, Chr(10),  , CompareMethod.Binary)
		End If
		
		Dim lngIndex As Integer

		Dim objLine, objComputerList As StringArr
		
		Dim lngSelectedColumn As Integer
		lngSelectedColumn = SelectedColumn
		
		objComputerList = New StringArr
		
		For lngIndex = 0 To UBound(aryBuffer)
			If Len(aryBuffer(lngIndex)) > 0 Then
				objLine = New StringArr
				
				Select Case mlngFileType
					Case NotDelimited
						objComputerList.Add(MyTrim(aryBuffer(lngIndex)))
					Case TabDelimited
						objLine.GenerateNewFromString(aryBuffer(lngIndex), Chr(9))
						objComputerList.Add(MyTrim(objLine.Item(lngSelectedColumn)))
					Case CommaDelimited
						objLine.GenerateNewFromString(aryBuffer(lngIndex), ",")
						objComputerList.Add(MyTrim(objLine.Item(lngSelectedColumn)))
				End Select
				
			End If
		Next 
		
		If chkStripQuotes.CheckState = System.Windows.Forms.CheckState.Checked Then
			For lngIndex = 0 To objComputerList.Count - 1
				objComputerList.Item(lngIndex) = MyCustomTrim(objComputerList.Item(lngIndex), """")
			Next 
		End If
		
		
		Dim lngStartIndex As Integer
		If chkHeaderRow.CheckState = System.Windows.Forms.CheckState.Checked Then
			lngStartIndex = 1
		Else
			lngStartIndex = 0
		End If
		
		If Len(frmMain.txtInputComputers.Text) > 0 Then
			'UPGRADE_WARNING: Couldn't resolve default property of object objComputerList.Glue(). Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
			frmMain.txtInputComputers.Text = frmMain.txtInputComputers.Text & vbCrLf & objComputerList.Glue(vbCrLf,  , lngStartIndex)
		Else
			'UPGRADE_WARNING: Couldn't resolve default property of object objComputerList.Glue(). Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
			frmMain.txtInputComputers.Text = frmMain.txtInputComputers.Text & objComputerList.Glue(vbCrLf,  , lngStartIndex)
		End If
		
		Me.Hide()
		
	End Sub
	
	Private Sub frmImport_Load(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles MyBase.Load
		mobjPreview = New GenObjArr
		mlngColumnCount = 0
		mlngFileType = 3
		SetGUIButtons()
	End Sub
	
	
	
	Private Sub cmdBrowse_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdBrowse.Click
		
		Dim strFileName As String
		
		CommonDialog1Open.Title = "Select File to Import"
		CommonDialog1Open.ShowDialog()
		
		strFileName = CommonDialog1Open.FileName
		
		If strFileName = "" Then
			SetGUIButtons()
			Exit Sub
		End If
		
		txtFileToImport.Text = strFileName
		
		ReadSome()
		
		Select Case mlngFileType
			Case TabDelimited
				txtFileType.Text = "Tab Delimited"
			Case CommaDelimited
				txtFileType.Text = "Comma Delimited"
			Case Else
				txtFileType.Text = "Non-Delimited"
		End Select
		
		If mobjPreview.Count > 0 Then
			'UPGRADE_WARNING: Couldn't resolve default property of object mobjPreview.Item().Count. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
			txtColumns.Text = mobjPreview.Item(0).Count
			
			'UPGRADE_WARNING: Couldn't resolve default property of object mobjPreview.Item().Item. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
			If mlngFileType = TabDelimited And mobjPreview.Item(0).Item(0) = "Name" Then
				chkHeaderRow.CheckState = System.Windows.Forms.CheckState.Checked
				'UPGRADE_WARNING: Couldn't resolve default property of object mobjPreview.Item().Item. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
			ElseIf mlngFileType = CommaDelimited And InStr(1, mobjPreview.Item(0).Item(0), "Netbios Name") > 0 Then 
				chkHeaderRow.CheckState = System.Windows.Forms.CheckState.Checked
			Else
				chkHeaderRow.CheckState = System.Windows.Forms.CheckState.Unchecked
			End If
		End If
		
		UpdateColumnNames()
		
		If lstColumnToImport.Items.Count > 0 Then
			lstColumnToImport.SetSelected(0, True) 'select first column by default...
		End If
		
		DisplayPreviewText()
		SetGUIButtons()
	End Sub
	
	Function SelectedColumn() As Integer
		Dim lngIndex As Integer
		SelectedColumn = -1
		For lngIndex = 0 To lstColumnToImport.Items.Count - 1
			If lstColumnToImport.GetSelected(lngIndex) = True Then
				SelectedColumn = lngIndex
				Exit Function
			End If
		Next 
	End Function
	
	
	Sub DisplayPreviewText()
		Dim lngIndex, lngSelectedColumn As Integer
		
		'Find Selected Column...
		If mlngFileType = NotDelimited Then
			lngSelectedColumn = 0
		Else
			lngSelectedColumn = SelectedColumn
			If lngSelectedColumn = -1 Then
				Exit Sub 'No selected column...
			End If
		End If
		
		lstTextToImport.Items.Clear()
		
		Dim lngStartIndex As Integer
		If chkHeaderRow.CheckState = System.Windows.Forms.CheckState.Checked Then
			lngStartIndex = 1
		Else
			lngStartIndex = 0
		End If
		
		For lngIndex = lngStartIndex To mobjPreview.Count - 1
			If chkStripQuotes.CheckState = System.Windows.Forms.CheckState.Checked Then
				'UPGRADE_WARNING: Couldn't resolve default property of object mobjPreview.Item().Item. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
				lstTextToImport.Items.Add(MyCustomTrim(mobjPreview.Item(lngIndex).Item(lngSelectedColumn), """"))
			Else
				'UPGRADE_WARNING: Couldn't resolve default property of object mobjPreview.Item().Item. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
				lstTextToImport.Items.Add(mobjPreview.Item(lngIndex).Item(lngSelectedColumn))
			End If
		Next lngIndex
		
		If mobjPreview.Count >= 10 Then
			lstTextToImport.Items.Add("<etc...>")
		End If
		
	End Sub
	
	
	
	Sub UpdateColumnNames()
		
		Dim lngColumnIndex As Integer
		Dim lngOldSelection As Integer
		
		lngOldSelection = SelectedColumn
		
		If mobjPreview.Count > 0 Then
			
			lstColumnToImport.Items.Clear()
			
			'UPGRADE_WARNING: Couldn't resolve default property of object mobjPreview.Item(0).Count. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
			For lngColumnIndex = 0 To mobjPreview.Item(0).Count - 1
				If chkHeaderRow.CheckState = System.Windows.Forms.CheckState.Checked Then
					If chkStripQuotes.CheckState = System.Windows.Forms.CheckState.Checked Then
						'UPGRADE_WARNING: Couldn't resolve default property of object mobjPreview.Item().Item. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
						lstColumnToImport.Items.Add(MyCustomTrim(mobjPreview.Item(0).Item(lngColumnIndex), """"))
					Else
						'UPGRADE_WARNING: Couldn't resolve default property of object mobjPreview.Item().Item. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
						lstColumnToImport.Items.Add(mobjPreview.Item(0).Item(lngColumnIndex))
					End If
				Else
					lstColumnToImport.Items.Add("Column " & CStr(lngColumnIndex))
				End If
			Next 
			
			If lngOldSelection > -1 And lstColumnToImport.Items.Count >= (lngOldSelection + 1) Then
				lstColumnToImport.SetSelected(lngOldSelection, True)
			ElseIf lstColumnToImport.Items.Count > 0 Then 
				lstColumnToImport.SetSelected(0, True)
			End If
			
		End If
		
	End Sub
	
	Private Sub ReadSome()
		
		Dim objFSO As Scripting.FileSystemObject
		objFSO = New Scripting.FileSystemObject
		
		Dim objInputFile As Scripting.TextStream
		objInputFile = objFSO.OpenTextFile(txtFileToImport.Text, Scripting.IOMode.ForReading, False, Scripting.Tristate.TristateUseDefault)
		
		Dim strLine As String
		Dim strLines As StringArr
		Dim lngIndex As Integer
		
		strLines = New StringArr
		
		'Read and process the first line...
		strLine = objInputFile.ReadLine
		If InStr(1, strLine, Chr(9), CompareMethod.Binary) > 0 Then
			mlngFileType = TabDelimited
			mlngColumnCount = OccurrenceCount(strLine, Chr(9)) + 1
		ElseIf InStr(1, strLine, ",", CompareMethod.Binary) > 0 Then 
			mlngFileType = CommaDelimited
			mlngColumnCount = OccurrenceCount(strLine, ",") + 1
		Else
			mlngFileType = NotDelimited
			mlngColumnCount = 1
		End If
		strLines.Add(strLine)
		
		'Read nine more lines...
		For lngIndex = 1 To 9
			If Not objInputFile.AtEndOfStream Then
				strLine = objInputFile.ReadLine
				strLines.Add(strLine)
			End If
		Next 
		
		objInputFile.Close()
		'UPGRADE_NOTE: Object objInputFile may not be destroyed until it is garbage collected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6E35BFF6-CD74-4B09-9689-3E1A43DF8969"'
		objInputFile = Nothing
		
		
		Dim objCurLine As StringArr
		
		mobjPreview = New GenObjArr
		For lngIndex = 0 To strLines.Count - 1
			objCurLine = New StringArr
			Select Case mlngFileType
				Case TabDelimited
					objCurLine.GenerateNewFromString(strLines.Item(lngIndex), Chr(9), True, True)
				Case CommaDelimited
					objCurLine.GenerateNewFromString(strLines.Item(lngIndex), ",", True, True)
				Case NotDelimited
					objCurLine.Add(strLines.Item(lngIndex))
			End Select
			mobjPreview.Add(objCurLine)
		Next 
		
		System.Windows.Forms.Application.DoEvents()
		
	End Sub
	
	'UPGRADE_WARNING: Event lstColumnToImport.SelectedIndexChanged may fire when form is initialized. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="88B12AE1-6DE0-48A0-86F1-60C0686C026A"'
	Private Sub lstColumnToImport_SelectedIndexChanged(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles lstColumnToImport.SelectedIndexChanged
		DisplayPreviewText()
	End Sub
	
	Private Sub SetGUIButtons()
		cmdImport.Enabled = (Len(txtFileToImport.Text) > 0 And lstTextToImport.Items.Count > 0)
	End Sub
	
	'UPGRADE_WARNING: Event txtFileToImport.TextChanged may fire when form is initialized. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="88B12AE1-6DE0-48A0-86F1-60C0686C026A"'
	Private Sub txtFileToImport_TextChanged(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles txtFileToImport.TextChanged
		SetGUIButtons()
	End Sub
End Class